/// DO THE SAME CHANGES TO DATABASE FOR 10G and 11G


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace GhostLogger
{
    public partial class frmDatabaseInfo : Form
    {

        #region Local Variables

        frmDebugInfo dbgInfo;
        
        #endregion

        #region Constructor
        public frmDatabaseInfo()
        {
            InitializeComponent();

            dbgInfo = new frmDebugInfo();
            dbgInfo.Show();
            dbgInfo.Enabled = false;
            //*************************************REMOVE*****************
            //txtSID.Text = "DS754";
            //txtUserName.Text = "SCANDEV";
            //txtPassword.Text = "SCANDEV";
            //*************************************REMOVE*****************
        }
        #endregion

        #region Button Clicks


        private void btnConnect_Click(object sender, EventArgs e)
        {

            string strDatasource = txtSID.Text;
            string strUsername = txtUserName.Text;
            string strPassword = txtPassword.Text;


            DBConnection.PrepareConnectString(strDatasource, strUsername, strPassword);

            if (DBConnection.Connect())
            {

                if (DBConnection.CheckInstallation())
                {
                    //MessageBox.Show("Ghost Logger exists in the database and can be used reused.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    MessageBox.Show("Connection successfully created.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Hide();
                    dbgInfo.Enabled = true;
                    dbgInfo.Focus();

                    DataSet addedPackages = DBConnection.RetrieveDebugAddedHistory();
                    foreach (DataRow theRow in addedPackages.Tables["c_debug_history_tab"].Rows)
                    {
                        DebugInfo.debugPackages.Add(theRow["package_name"]);
                    }

                }
                else
                {

                    DialogResult dlgResult = MessageBox.Show("Database packages has not been deployed to this database. Do you want to deploy them now?", "No Database Packages", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (dlgResult == DialogResult.Yes)
                    {
                        DBConnection.Install(strDatasource, strUsername, strPassword);
                        //MessageBox.Show("Application will be restarted.", "Restart", MessageBoxButtons.OK, MessageBoxIcon.Information);           
                        //Application.Restart();                        
                        btnConnect.PerformClick();
                    }
                    else
                    {
                        Application.Exit();
                    }
                }
            }
            else
            {
                MessageBox.Show("Error occured while connecting to database. Please check the input parameters.", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        
        #endregion

        #region Keypress Events of Text fields

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                btnConnect.PerformClick();
            }
        }

        
        #endregion
        
    }
}